package com.web.controller.api;

import com.web.annotation.UserAccess;
import com.web.dao.BankMapper;
import com.web.dao.MemberMapper;
import com.web.domain.ApiResponse;
import com.web.entity.Bank;
import com.web.entity.Member;
import com.web.manager.BankManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author FDjavaone
 * @date 2017/8/23
 */

@RestController
@RequestMapping("api")
public class BankController {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private BankMapper bankMapper;

    /**
     * 银行卡信息获取
     */
    @RequestMapping("/app/bank/getBankInfo")
    public ApiResponse authIdCard(Bank bank) {
        return BankManager.getBankCardInfo(bank);
    }

    /**
     * 银行卡添加
     */
    @UserAccess
    @RequestMapping("/app/bank/add")
    public ApiResponse add(Bank bank, HttpServletRequest req, HttpServletResponse resp) {
        Member member = memberMapper.selectByPrimaryKey(bank.getmId());
        ApiResponse response = BankManager.checkBank(bank, member);
        try {
            if (response.getStatus() == 900000) {
                Date date = new Date();
                bank.setCreateTime(date);
                bank.setModifyTime(date);
                bank.setName(member.getName());
                bankMapper.insertSelective(bank);
                List<Bank> list = bankMapper.selectByMid(bank.getmId());
                response.setParams(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.setMessage("银行卡已被添加！");
            response.setStatus(900116);
        }
        return response;
    }

    /**
     * 银行卡列表获取
     */
    @UserAccess
    @RequestMapping("/app/bank/list")
    public ApiResponse list(Bank bank, HttpServletRequest req, HttpServletResponse resp) {
        ApiResponse response = new ApiResponse();
        List<Bank> list = bankMapper.selectByMid(bank.getmId());
        response.setMessage("银行卡获取成功！");
        response.setStatus(900000);
        response.setParams(list);
        return response;
    }

    @UserAccess
    @RequestMapping("/app/bankCard/delete")
    public ApiResponse unBindBankCard(Bank bank, HttpServletRequest req, HttpServletResponse resp) {
        ApiResponse response = new ApiResponse();
        bankMapper.deleteByPrimaryKey(bank.getId());
        List<Bank> list = bankMapper.selectByMid(bank.getmId());
        response.setParams(list);
        response.setMessage("银行卡删除成功！");
        response.setStatus(900000);
        return response;
    }

    /**
     * 切换默认银行卡
     */
    @UserAccess
    @RequestMapping("/app/bank/bankSwitch")
    public ApiResponse addressSwitch(Bank bank, HttpServletRequest req, HttpServletResponse resp) {
        ApiResponse response = new ApiResponse();
        Date date = new Date();
        bank.setModifyTime(date);
        bank.setDefaultStatus(0);
        bankMapper.setAllAddressNotDefault(bank);
        bank.setDefaultStatus(1);
        bankMapper.updateByPrimaryKeySelective(bank);
        List<Bank> list = bankMapper.selectByMid(bank.getmId());
        response.setParams(list);
        response.setMessage("默认银行卡设置成功！");
        response.setStatus(900000);
        return response;
    }

}
